package me.aartikov.flowscreenswitchersample.ui;

import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.Screen;
import me.aartikov.alligator.annotations.RegisterScreen;
import me.aartikov.alligator.helpers.MiFraction;
import me.aartikov.alligator.helpers.FractionStack;
import me.aartikov.flowscreenswitchersample.ResourceTable;
import me.aartikov.flowscreenswitchersample.SampleApplication;
import me.aartikov.flowscreenswitchersample.screens.InnerScreen;
import me.aartikov.flowscreenswitchersample.screens.TabScreen;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import java.util.List;

@RegisterScreen(TabScreen.class)
public class TabMiFraction extends MiFraction implements ContainerIdProvider {
    Text mNameTextView;


    private Navigator mNavigator = SampleApplication.getNavigator();

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        mNavigator = SampleApplication.getNavigator();
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_mifraction_tab, null, false);
        mNameTextView = (Text) component.findComponentById(ResourceTable.Id_name_text_view);

        TabScreen screen = SampleApplication.getScreenResolver().getScreen(this);
        mNameTextView.setText(screen.getName());
        return component;
    }

    @Override
    protected void setInitialFragmentIfRequired() {
        List<Screen> screens = FractionStack.getInstance(getFractionAbility().getFractionManager()).getCurrentFragmentScreen(this.toString());
        if (screens.size() == 0 && mNavigator.canExecuteCommandImmediately()) {
            mNavigator.goForward(new InnerScreen(1, getContainerId(), this.toString()));
        } else {
            mNavigator.reduce(screens);
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    public int getContainerId() {
        return ResourceTable.Id_inner_container;
    }


}
